﻿<%@ WebHandler Language="C#" Class="GetData" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Collections;
using System.Web.Script.Serialization;

public class GetData : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
{
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/json";
        string strName = context.Request["name"];
        JavaScriptSerializer js = new JavaScriptSerializer();
        if (context.Session["BRANCH_ID"] != null)
        {
            string strBranchID = context.Session["BRANCH_ID"].ToString();
            if (strName.Equals("all"))
            {
                context.Response.Write(js.Serialize(GetAllContent(strBranchID)));
            }
            else
            {
                context.Response.Write(js.Serialize(GetAllContentByName(strName, strBranchID)));
            }
        }
        else
        {
            context.Response.Write("SessionTimeout");
        }
    }

    private ArrayList GetAllContent(string strBranchID)
    {
        ArrayList modelList = new ArrayList();
        BLL.SysManage.T_PUB_DICTIONARY bll = new BLL.SysManage.T_PUB_DICTIONARY();
        System.Collections.Generic.List<Model.SysManage.T_PUB_DICTIONARY> list = bll.GetAddress(strBranchID,"");
        foreach (Model.SysManage.T_PUB_DICTIONARY item in list)
        {
             Name name = new Name();
             name.id = item.ID.ToString();
             name.s = item.NAME;
             modelList.Add(name);
        }
        return modelList;
    }

    private ArrayList GetAllContentByName(string strName, string strBranchID)
    {
        ArrayList modelList = new ArrayList();
        BLL.SysManage.T_PUB_DICTIONARY bll = new BLL.SysManage.T_PUB_DICTIONARY();
        System.Collections.Generic.List<Model.SysManage.T_PUB_DICTIONARY> list = bll.GetAddress(strBranchID, strName);
        foreach (Model.SysManage.T_PUB_DICTIONARY item in list)
        {
            Name name = new Name();
            name.id = item.ID.ToString();
            name.s = item.NAME;
            modelList.Add(name);
        }
        return modelList;
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

    public class Name
    {
        public string s = "";
        public string id = "";
    }
}